
const Round =qguan(0,"Round")
const AnalogyChange =qguan(0,"AnalogyChange")
const Row =qguan(0,"Row")


//import Core from "../Core";

export default class Instruction{
    constructor(code,pThis){
        this.pThis=pThis;
        this.codePointer=1;
        this.dataPointer=new Row(0,0,0,0);
        this.temPointer=new Row(0,0,0,0);
        this.code=code;
        this.stack=new Array();
        this.temData=new Round();
    }

    /**
     * 执行
     */
    /**
     * 111 111 111 0    判断 跳转
     * 111 111 111 1  无条件跳转
     * 111 111 111 2
     * 111 111 111 3  输入
     * 111 111 111 4  输出
     * 111         5  立即数输入
     *             6  空操作
     * 1           7  立即数加
     * 1           8  立即数减
     * 1           10 创建块 入栈
     * 1           11 创建行
     *  1          12  内调
     *            13  入栈
     *            14  读取指令指针
     *            15 外调
     * 1           16 删除行
     * 1           17 删除块
     * 1           18 读取代码段
     * 1           19 写入代码段
     * 1           20 读取数据段
     * 1           21 写入数据段
     * 1           22 修改输出状态
     *             23 结束程序
     *             24 设置输出节点
     *              25 获取顶级节点的长度
     *              26 用寄存机里面的查找节点数据
     */

    run(){
        let _this=this;


        while (_this.codePointer<_this.code.list[0].list.length+1 &&_this.stack.length<=0){
            if(_this.codePointer>_this.code.list[0].list.length){
                _this.pop(); //退栈
                _this.codePointer++;
                continue;
            }

            let row=_this.code.findData(0,0,null,_this.func.bind(_this))
            let a1=row.find(1);
            let a2=row.find(2);
            let a3=row.find(3);
            let a4=row.find(4);

            switch (a4){
                case '0': // 名词
                    break;
                case '1': //结构
                    _this.structure(a1,a2,a3,a4);
                    break;
                case '2': //动作
                    _this.operate(a1,a2,a3,a4);
                    break;
                default : // 入栈调用别的程序
                    _this.push(a1,a2,a3,a4);
                    break;
            }


            _this.codePointer++
        }
    }

    rowLength(RoundId,BlockId,RowId,dataThis){
        return dataThis.list.length
    }

    func(RoundId,BlockId,RowId,dataThis){
       return   dataThis.list[this.codePointer-1]
    }

    findRow(RoundId,BlockId,RowId,dataThis){
        return   dataThis.list[0]
    }

    /**
     * 结构
     */
    structure(){
        //读取解析参数
        let part= this.code.list[this.codePointer];
        //有没有条件
        let ifPointer=0;
        let ifRow=null
        let isIf=false;
         for(let i=0;i<part.list.length;i++){
             let row=part.list[i];
             if(row.isValue(4,"1")){ //对比方式
                 contrastType=row;
                 continue;
             }
             if(row.isValue(4,"2")){ //参数 库
                 param=row;
                 continue;
             }
             if(row.isValue(4,"3")){ //参数位置
                 param=row;
                 continue;
             }
             if(row.isValue(4,"4")){ //源 库
                 resType=row;
                 continue;
             }
             if(row.isValue(4,"5")){ //源 位置
                 resType=row;
                 continue;
             }
             if(row.isValue(3,"6")){ //有条件跳转
                 ifRow=row;
                 continue;
             }
             if(row.isValue(3,"7")){//无条件跳转
                 ifPointer=row.find(1)
                 continue;
             }
         }

         if(isIf){ //执行判断
             if(ifRow){
                 if(this.isIf()){ //条件判断
                     ifPointer=ifRow.find(2)
                 }else {
                     ifPointer=ifRow.find(1)
                 }
             }
         }
         if(ifPointer===0){
             this.codePointer=this.code.length;
         }else {

             this.codePointer=ifPointer;
         }


        //跳转


    }
    isIf(){

        return true;
    }
    /**
     * 操作
     *  参数类型
     *  返回值类型
     *  操作数据
     */
    operate(a1,a2,a3,a4){

        //读取解析参数
        let part= this.code.list[this.codePointer];

        //条件
        //有没有条件
        let resType=null; //返回类型
        let ergodic=null; //遍历方式
        let contrastType=null;//对比方式
        let param=null;//参数位置

        let paramRound=null;//参数位置
        let source=null;//源

        let sourceRound="0";//
        let target=null;//目标
        let targetRound="2";
        for(let i=0;i<part.list.length;i++){
            let row=part.list[i];
            if(row.isValue(4,"1")){ //对比方式
                this.contrastType=row;
                continue;
            }
            if(row.isValue(4,"2")){ //参数 库
                this.paramRound=row;
                continue;
            }
            if(row.isValue(4,"3")){ //参数位置
                this. param=row;
                continue;
            }
            if(row.isValue(4,"4")){ //源 库
               sourceRound=row.find(1);
                continue;
            }
            if(row.isValue(4,"5")){ //源 位置
              source=row;
                continue;
            }

            if(row.isValue(4,"6")){ //目标 库
               targetRound=row.find(1);
                continue;
            }
            if(row.isValue(4,"7")){ //目标位置
                this. target=row;
                continue;
            }
            if(row.isValue(4,"8")){//遍历方式
                this. ergodic=row;
                continue;
            }

        }

        /**
         *参数类型和位置
         */
        switch (param){

        }

        /**
         * 原数据执行查询
         */
        let res;
        if(!source){
            source=new Row(0,0,0,0)
        }
        switch (sourceRound){

            case "1":{
                res= this.pThis.findData(source.find(1),source.find(2),source.find(3),this.tree.bind(this));
                break
            }
            case "2":{
                break
            }
            case "3":{
                break
            }
            case "4":{
                break
            }
            default:{
                res=   this.pThis.findData(source.find(1),source.find(2),source.find(3),this.tree.bind(this));
            }

        }
        /**
         * 目标
         */
        if(!target){
            target=new Row(0,0,0,0)
        }
        switch (targetRound){
            case "1":{
                 this.saveData(target.find(1),target.find(2),target.find(3),res);
                break
            }
            case "2":{
                this.pThis.saveOutType(res);
                break
            }

            default:{
                this.temData.saveData(target.find(1),target.find(2),target.find(3),res);
                break
            }
        }

    }

    tree(fileId,blockId,rowId,dataThis){

        return      dataThis.list[1];
    }


    /**
     *            13  入栈
     **/
    push(a1,a2,a3,a4){
        // 保存环境到栈里面
        let environment={};
        environment.codePointer=this.codePointer;
        environment.dataPointer=this.dataPointer;
        environment.temPointer=this.temPointer;
        environment.code=this.code;
        environment.temData=this.temData;
        this.stack.push(environment);

        //获取代码 创建新环境
        let core= Core.instance();
        let codeBocik=core.findReal(null,'44');
        let codeRound=AnalogyChange.toRound(codeBocik);

        this.codePointer=1;
        this.dataPointer=new Row(0,0,0,0);
        this.temPointer=new Row(0,0,0,0);
        this.code=codeRound;
        this.temData=new Round();

    }
    /**
     * 1           14 弹出栈
     **/
    pop(a1,a2,a3){
        let environment=  this.stack[this.stack.length-1]
        let temData=this.temData;

        this.codePointer=environment.codePointer;
        this.dataPointer=environment.dataPointer;
        this.temPointer=environment.temPointer;
        this.code=environment.code;
        this.temData=environment.temData;

        //把执行结果，还原栈环境
        let res= temData.list[temData.list.length-1];

    }

    /**
     *  返回值 处理
     * @param a1
     * @param a2
     * @param a3
     */
    code15(a1,a2,a3){

    }


    /*/!**
    * 111 111 111 0  跳转
    *!/
    code0(a1,a2,a3){
        if(this.register==0){
            return;
        }
        this.codePointer=a1;
    }
    /!**
     *  解析参数
     **!/
    code1(a1,a2,a3){

        let res=    this.pThis.isVls(a1,a2,a3,this.register);
        if(res){
            this.register=1;
        }else {
            this.register=0;
        }
    }
    /!**
    * 111 111 111 2  判断
    **!/
    code2(a1,a2,a3){

       let res=    this.pThis.isVls(a1,a2,a3,this.register);
       if(res){
           this.register=1;
       }else {
           this.register=0;
       }
    }

    /!**
    * 111 111 111 3  移动
    **!/
    code3(a1,a2,a3){

        this.register=    this.pThis.findVls(a1,a2,a3,this.register);
    }
    /!**
    * 1           7   加
    **!/
    code7(a1,a2,a3){

       this.register=  this.register+a1;
    }
    /!**
    * 1           8   减
    **!/
    code8(a1,a2,a3){


        this.register=  this.register-a1;
    }

    /!**
    * 1           10 添加块
    **!/
    code10(a1,a2,a3){

        this.stack.addData(0,null,null,new Block());
    }
    /!**
    * 1           11 添加行
    **!/
    code11(a1,a2,a3,a4){

        this.stack.addData(0,this.stack.list.length-1,null,new Row(a1,a2,a3,a4));
    }*/

   /* /!**
    * 1           16 删除行
    **!/
    code16(a1,a2,a3){
        this.register=a2;
        this.stack.findData(0,a1,null,this._code16.bind(this))

    }
    _code16(RoundId,BlockId,RowId,dataThis){
        delete   dataThis.list[this.register]
    }
    /!**
    * 1           17 删除块
    **!/
    code17(a1,a2,a3){
        this.register=a2;
        this.stack.findData(0,null,null,this._code17.bind(this))
    }
    _code17(RoundId,BlockId,RowId,dataThis){
         delete   dataThis.list[this.register]
    }

    /!**
    * 1           22 修改输出状态
    **!/
    code22(a1,a2,a3){
        this.pThis.saveOutType(a1);

    }*/
}